package cn.school.service;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.school.dao.StudentDao;
import cn.school.entity.Page;
import cn.school.entity.Student;
import cn.school.util.MyBatisUtil;

public class Service {
	public List<Student> searchLimitByMohu(int pageno, int pagesize, String sname, String telephone, String qq,
			Date enterdatestart) {
		SqlSession session = MyBatisUtil.getSqlsession();
		try {
			StudentDao studentDao = session.getMapper(StudentDao.class);
			return studentDao.searchLimitByMohu(pageno, pagesize, sname, telephone, qq, enterdatestart);
		} finally {
			MyBatisUtil.getclose(session);
		}
	}

	public List<Student> getAll() {
		SqlSession session = MyBatisUtil.getSqlsession();
		try {
			StudentDao studentDao = session.getMapper(StudentDao.class);
			return studentDao.getAll();
		} finally {
			MyBatisUtil.getclose(session);
		}
	}

	public Student getOne(int stuno) {
		SqlSession session = MyBatisUtil.getSqlsession();
		try {
			StudentDao studentDao = session.getMapper(StudentDao.class);
			return studentDao.getOne(stuno);
		} finally {
			MyBatisUtil.getclose(session);
		}
	}

	public int update(Student stu) {
		SqlSession session = MyBatisUtil.getSqlsession();
		try {
			StudentDao studentDao = session.getMapper(StudentDao.class);
			return studentDao.update(stu);
		} finally {
			MyBatisUtil.getclose(session);
		}
	}

	public int deleteByStuId(long stuno) {
		SqlSession session = MyBatisUtil.getSqlsession();
		try {
			StudentDao studentDao = session.getMapper(StudentDao.class);
			return studentDao.deleteByStuId(stuno);
		} finally {
			MyBatisUtil.getclose(session);
		}
	}

	public int getTotalCount() {
		SqlSession session = MyBatisUtil.getSqlsession();
		try {
			StudentDao studentDao = session.getMapper(StudentDao.class);
			return studentDao.getTotalCount();
		} finally {
			MyBatisUtil.getclose(session);
		}
	}

	public int add(Student stu) {
		SqlSession session = MyBatisUtil.getSqlsession();
		try {
			StudentDao studentDao = session.getMapper(StudentDao.class);
			return studentDao.add(stu);
		} finally {
			MyBatisUtil.getclose(session);
		}
	}

	public int searchTotal(String sname, String telephone, String qq, Date enterdatestart) {
		SqlSession session = MyBatisUtil.getSqlsession();
		try {
			StudentDao studentDao = session.getMapper(StudentDao.class);
			return studentDao.searchTotal(sname, telephone, qq, enterdatestart);
		} finally {
			MyBatisUtil.getclose(session);
		}
	}

	public boolean checkTele(String tele) {
		SqlSession s = MyBatisUtil.getSqlsession();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			return studentDao.checkTele(tele) > 0;
		} finally {
			MyBatisUtil.getclose(s);
			;
		}
	}

	public final int PAGE_SIZE = 20;

	public Page<Student> paging(int pageNo, String name, String tele, String qq, Date enterDateStart) {
		SqlSession s = MyBatisUtil.getSqlsession();
		try {
			StudentDao studentDao = s.getMapper(StudentDao.class);
			List<Student> list = studentDao.searchLimitByMohu(pageNo, PAGE_SIZE, name, tele, qq, enterDateStart);
			int total = studentDao.getTotalCount();

			return new Page<Student>(pageNo, total, PAGE_SIZE, list);
		} finally {
			MyBatisUtil.getclose(s);
			;
		}
	}

}
